import { _decorator, Component, Node, Button, Label, find } from 'cc';
import { PropType } from '../Controller/PropController';
import { GameManager } from '../Manager/GameManager';
const { ccclass, property } = _decorator;

@ccclass('PropItem')
export class PropItem extends Component {

    button: Button = null!;

    countLb: Label = null!;

    propType: PropType;
    count: number;

    unLock: boolean = true;

    onLoad() {
        this.countLb = find("CountBG/CountLb", this.node).getComponent(Label);
        this.button = this.node.getComponent(Button);
    }

    Init(propType: PropType, count: number) {
        this.propType = propType;

        this.RefreshCount(count);

        //按钮回调
        this.button.clickEvents = [];
        let clickEventHandler = new Component.EventHandler();
        clickEventHandler.target = this.node;//对应的组件node
        clickEventHandler.component = `PropItem`;  //对应的组件脚本
        clickEventHandler.handler = `OnBtnClick`;        //回调
        //clickEventHandler.customEventData = itemData;     //数据
        this.button.clickEvents.push(clickEventHandler);
    }

    RefreshCount(count: number) {
        this.count = count;
        this.countLb.string = `${count}`;
    }

    OnBtnClick() {
        GameManager.Instance.UseProp(this.propType);
    }

}


